package com.glsc.ngateway.platform.controller.cmdb;

import com.glsc.ngateway.common.api.platform.dto.user.PlatformUserDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbDeviceDetail;
import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbDeviceStandard;
import com.glsc.ngateway.common.base.dto.cmdb.*;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.platform.service.cmdb.*;
import com.glsc.ngateway.platform.utils.AssertOverride;
import com.glsc.ngateway.platform.utils.report.ReportFileConfigEnum;
import com.glsc.ngateway.platform.utils.report.ReportFileUtil;
import com.glsc.ngateway.platform.utils.report.ReportUploadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/cmdb/excel")
@Api(value = "cmdb导入导出", tags = {"cmdb导入导出"})
public class CmdbImportExportController {


    @Autowired
    private CmdbExcelImportExportService cmdbExcelImportExportService;

    @Autowired
    private UserCache userCache;
    @Autowired
    private HttpServletRequest request;
    @Resource
    private CmdbCpuStandardService cmdbCpuStandardService;
    @Resource
    private CmdbMemoryStandardService cmdbMemoryStandardService;

    @Resource
    private CmdbCpuDetailService cmdbCpuDetailService;
    @Resource
    private CmdbMemoryDetailService cmdbMemoryDetailService;
    @Resource
    private CmdbDiskDetailService cmdbDiskDetailService;
    @Resource
    private CmdbPartDetailService cmdbPartDetailService;


    @PostMapping("/upload/standard/cpu")
    @ApiOperation(value = "cpu规格导入", notes = "cpu规格导入")
    public PlatformResponse<?> uploadCpuStandard(@RequestPart("file") MultipartFile file, @RequestParam("type") Integer type) throws IOException {
        PlatformUserDto user = userCache.get(request);
        ReportUploadContext<CmdbCpuStandardImportDto, String> context = cmdbExcelImportExportService.cpuStandardUpload(file, type, user);
        return context.isCheckPass() ? PlatformResponse.successData(context.getReadRes()) : PlatformResponse.failedMsg(String.join(",", context.getCheckRes()));
    }

    @PostMapping("/upload/standard/memory")
    @ApiOperation(value = "内存规格导入", notes = "内存规格导入")
    public PlatformResponse<?> uploadMemoryStandard(@RequestPart("file") MultipartFile file, @RequestParam("type") Integer type) throws IOException {
        PlatformUserDto user = userCache.get(request);
        ReportUploadContext<CmdbMemoryStandardImportDto, String> context = cmdbExcelImportExportService.memoryStandardUpload(file, type, user);
        return context.isCheckPass() ? PlatformResponse.successData(context.getReadRes()) : PlatformResponse.failedMsg(String.join(",", context.getCheckRes()));
    }

    @PostMapping("/upload/standard/disk")
    @ApiOperation(value = "硬盘规格导入", notes = "硬盘规格导入")
    public PlatformResponse<?> uploadDiskStandard(@RequestPart("file") MultipartFile file, @RequestParam("type") Integer type) throws IOException {
        PlatformUserDto user = userCache.get(request);
        ReportUploadContext<CmdbDiskStandardImportDto, String> context = cmdbExcelImportExportService.diskStandardUpload(file, type, user);
        return context.isCheckPass() ? PlatformResponse.successData(context.getReadRes()) : PlatformResponse.failedMsg(String.join(",", context.getCheckRes()));
    }

    @PostMapping("/upload/standard/part")
    @ApiOperation(value = "配件规格导入", notes = "配件规格导入")
    public PlatformResponse<?> uploadPartStandard(@RequestPart("file") MultipartFile file, @RequestParam("type") Integer type) throws IOException {
        PlatformUserDto user = userCache.get(request);
        ReportUploadContext<CmdbPartStandardImportDto, String> context = cmdbExcelImportExportService.partStandardUpload(file, type, user);
        return context.isCheckPass() ? PlatformResponse.successData(context.getReadRes()) : PlatformResponse.failedMsg(String.join(",", context.getCheckRes()));
    }

    @PostMapping("/upload/standard/device")
    @ApiOperation(value = "服务器规格导入", notes = "服务器规格导入")
    public PlatformResponse<?> uploadDeviceStandard(@RequestPart("file") MultipartFile file, @RequestParam("type") Integer type) throws IOException {
        PlatformUserDto user = userCache.get(request);
        List<CmdbDeviceStandard> deviceStandards = cmdbExcelImportExportService.deviceStandardUpload(file, type, user);
        return PlatformResponse.successData(deviceStandards);
    }

    @PostMapping("/upload/detail/cpu")
    @ApiOperation(value = "cpu详情导入", notes = "cpu详情导入")
    public PlatformResponse<?> uploadCpuDetail(@RequestPart("file") MultipartFile file, @RequestParam("type") Integer type) throws IOException {
        PlatformUserDto user = userCache.get(request);
        ReportUploadContext<CmdbCommonDetailImportDto, String> context = cmdbExcelImportExportService.cpuDetailUpload(file, type, user);
        return context.isCheckPass() ? PlatformResponse.successData(context.getReadRes()) : PlatformResponse.failedMsg(String.join(",", context.getCheckRes()));
    }

    @PostMapping("/upload/detail/memory")
    @ApiOperation(value = "内存详情导入", notes = "内存详情导入")
    public PlatformResponse<?> uploadMemoryDetail(@RequestPart("file") MultipartFile file, @RequestParam("type") Integer type) throws IOException {
        PlatformUserDto user = userCache.get(request);
        ReportUploadContext<CmdbCommonDetailImportDto, String> context = cmdbExcelImportExportService.memoryDetailUpload(file, type, user);
        return context.isCheckPass() ? PlatformResponse.successData(context.getReadRes()) : PlatformResponse.failedMsg(String.join(",", context.getCheckRes()));
    }

    @PostMapping("/upload/detail/disk")
    @ApiOperation(value = "硬盘详情导入", notes = "硬盘详情导入")
    public PlatformResponse<?> uploadDiskDetail(@RequestPart("file") MultipartFile file, @RequestParam("type") Integer type) throws IOException {
        PlatformUserDto user = userCache.get(request);
        ReportUploadContext<CmdbCommonDetailImportDto, String> context = cmdbExcelImportExportService.diskDetailUpload(file, type, user);
        return context.isCheckPass() ? PlatformResponse.successData(context.getReadRes()) : PlatformResponse.failedMsg(String.join(",", context.getCheckRes()));
    }

    @PostMapping("/upload/detail/part")
    @ApiOperation(value = "配件详情导入", notes = "配件详情导入")
    public PlatformResponse<?> uploadPartDetail(@RequestPart("file") MultipartFile file, @RequestParam("type") Integer type) throws IOException {
        PlatformUserDto user = userCache.get(request);
        ReportUploadContext<CmdbCommonDetailImportDto, String> context = cmdbExcelImportExportService.partDetailUpload(file, type, user);
        return context.isCheckPass() ? PlatformResponse.successData(context.getReadRes()) : PlatformResponse.failedMsg(String.join(",", context.getCheckRes()));
    }


    @PostMapping("/upload/detail/device")
    @ApiOperation(value = "服务器详情导入", notes = "服务器详情导入")
    public PlatformResponse<?> uploadDeviceDetail(@RequestPart("file") MultipartFile file, @RequestParam("type") Integer type) throws IOException {
        PlatformUserDto user = userCache.get(request);
        ReportUploadContext<CmdbDeviceDetailImportDto, String> context = cmdbExcelImportExportService.deviceDetailUpload(file, type, user);
        return context.isCheckPass() ? PlatformResponse.successData(context.getReadRes()) : PlatformResponse.failedMsg(String.join(",", context.getCheckRes()));
    }


    @PostMapping("/upload/device/scrap")
    @ApiOperation(value = "服务器报废导入", notes = "服务器报废导入")
    public PlatformResponse<?> uploadDeviceScrap(@RequestPart("file") MultipartFile file) throws IOException {
        List<CmdbDeviceDetail> cmdbDeviceDetails = cmdbExcelImportExportService.deviceScrapUpload(file);
        return PlatformResponse.successData(cmdbDeviceDetails);
    }

    /**
     * @param file
     * @param type cpu memory disk part
     * @return
     * @throws IOException
     */
    @PostMapping("/upload/part/scrap")
    @ApiOperation(value = "配件报废导入", notes = "配件报废导入")
    public PlatformResponse<?> uploadPartScrap(@RequestPart("file") MultipartFile file, @RequestParam("type") String type) throws IOException {
        ReportUploadContext<CmdbDeviceScrapImportDto, String> context = ReportUploadContext.<CmdbDeviceScrapImportDto, String>builder()
                .config(ReportFileConfigEnum.CMDB_DEVICE_SCRAP)
                .file(file)
                .build();
        ReportFileUtil.readSingleExcel(context);
        AssertOverride.notEmpty(context.getReadRes(), "上传内容不可为空");
        List<String> snList = context.getReadRes().stream().map(CmdbDeviceScrapImportDto::getSn).collect(Collectors.toList());

        if (type.equals("cpu")) {
            return PlatformResponse.successData(cmdbCpuDetailService.listBySn(snList));
        } else if (type.equals("memory")) {
            return PlatformResponse.successData(cmdbMemoryDetailService.listBySn(snList));
        } else if (type.equals("disk")) {
            return PlatformResponse.successData(cmdbDiskDetailService.listBySn(snList));
        } else if (type.equals("part")) {
            return PlatformResponse.successData(cmdbPartDetailService.listBySn(snList));
        }
        return PlatformResponse.failedMsg("type类型异常");
    }


}